package com.tianji.tools;


import cn.hutool.json.JSONUtil;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.course.CourseBaseInfoDTO;
import com.tianji.constants.Constant;
import com.tianji.tools.result.CourseInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
@RequiredArgsConstructor
public class CourseTools {

    private final CourseClient courseClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据课程id查询课程信息
     *
     * @param courseId 课程id
     * @return 课程信息
     */
    @Tool(description = Constant.Tools.QUERY_COURSE_BY_ID)
    public CourseInfo queryCourseById(@ToolParam(description = Constant.ToolParams.COURSE_ID) Long courseId,
                                      ToolContext toolContext) {
        CourseBaseInfoDTO courseBaseInfoDTO = courseClient.baseInfo(courseId, true);
        CourseInfo courseInfo = CourseInfo.of(courseBaseInfoDTO);

        //准备返回之前，在Redis中添加requestId标记,以及响应的结果，表示该请求已经调用了Tools
        Map<String, Object> map = toolContext.getContext();
        String requestId = (String) map.get("requestId");
        stringRedisTemplate.opsForHash().put("AIGC:TOOLS",requestId, JSONUtil.toJsonStr(courseInfo));
        return courseInfo;
    }
}